package com.xhx.accounting;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;

import com.xhx.accounting.ui.home.HomeFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class search extends AppCompatActivity {

    private DBDao db;//数据库DAO
    private RecyclerView resultList;//lv
    private ListViewAdapter adapter;//lv的是配置
    private List<Map<String, Object>> dataList = new ArrayList<>();//datalist数据集，也就是lv的内容
    private EditText searchTextEdit;
    private int accountbookId;

    private enum LayoutManagerType {
        GRID_LAYOUT_MANAGER,
        LINEAR_LAYOUT_MANAGER
    }
    protected RecyclerView.LayoutManager mLayoutManager;
    protected LayoutManagerType mCurrentLayoutManagerType;
    private searchListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Intent intent = getIntent();
        accountbookId = intent.getIntExtra("accountId",-1);

        db = new DBDao(search.this);//新建数据库dao实例
        resultList = findViewById(R.id.resultList);//指定listview
        mLayoutManager = new LinearLayoutManager(this);
        mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
        resultList.setLayoutManager(mLayoutManager);
        resultList.scrollToPosition(0);
        mAdapter = new searchListAdapter(dataList);
        resultList.setAdapter(mAdapter);

        searchTextEdit = findViewById(R.id.search_text);//指定et
        searchTextEdit.setFocusable(true);
        searchTextEdit.setFocusableInTouchMode(true);
        searchTextEdit.requestFocus();
        searchTextEdit.setOnEditorActionListener((v, actionId, event) -> {//设置键盘搜索键和回车的监听
            if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {//如果有按下搜索/回车的order
                String searchText = searchTextEdit.getText().toString().trim();//获得et中的文字
                dataList.clear();//datalist设定为空
                if (searchText.length() != 0) {//如果et中有内容
                    hidekeyboard(v);
                    Log.d("Search",searchText);
                    dataList.addAll(db.searchRecord(searchText,accountbookId));
                    Log.d("searchRes",dataList.toString());
                    //通知recycleViews
                    mAdapter.notifyDataSetChanged();
                } else {
                    //
                }
                return true;//结束
            }
            return false;//结束
        });

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.arrow_left:
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            search.this.finish();
        }
        return super.onKeyUp(keyCode, event);
    }

    public void hidekeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        displayUtils.hideInputWhenTouchOtherView(this, ev, null);
        Log.d("TAG", "dispatchTouchEvent: 1");
        return super.dispatchTouchEvent(ev);
    }
}